//
//  RechargeViewController.m
//  Manage
//
//  Created by 杨康 on 2018/1/10.
//  Copyright © 2018年 杨康. All rights reserved.
//

#import "RechargeViewController.h"
#import <AlipaySDK/AlipaySDK.h>
#import "WXApi.h"
@interface RechargeViewController ()
{
    UIButton *_selectNumButton;
    UIButton *_selectButton;
}
@end

@implementation RechargeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title=@"充值余额";
    
    
    UILabel *titleLab=[SingLeton myLabel:[UIColor color6] andTitle:@"充值余额" andFont:14];
    [self.view addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(30+TOP_HEIGHT);
    }];
    
    
    CGFloat width=(SCREEN_WIDTH-45)/2;
    NSArray *moneyArr=[NSArray arrayWithObjects:@"充100元",@"充50元",@"充20元",@"充10元", nil];
    for (int i=0; i<4; i++) {
        NSInteger a=i/2;
        NSInteger b=i%2;
        UIButton *numButton=[UIButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:numButton];
        [numButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15+b*(width+15));
            make.top.mas_equalTo(titleLab.mas_bottom).mas_offset(10+75*a);
            make.size.mas_equalTo(CGSizeMake(width, 60));
        }];
        [numButton setTitle:moneyArr[i] forState:UIControlStateNormal];
        [numButton setTitleColor:[UIColor colorWithHexString:@"222222"] forState:UIControlStateNormal];
        [numButton setTitleColor:[UIColor colorWithHexString:@"09be00"] forState:UIControlStateSelected];
        numButton.layer.masksToBounds=YES;
        numButton.layer.cornerRadius=5;
        numButton.layer.borderWidth=1;
        numButton.layer.borderColor=[UIColor colorWithHexString:@"09be00"].CGColor;
        numButton.titleLabel.font=[UIFont systemFontOfSize:17.0];
        
        [numButton addTarget:self action:@selector(chooseAction:) forControlEvents:UIControlEventTouchUpInside];
        if (i==0) {
            [numButton setBackgroundImage:[UIImage imageNamed:@"充值金额背景"] forState:UIControlStateSelected];
            [self chooseAction:numButton];
        }else
        {
          [numButton setBackgroundImage:[UIImage imageNamed:@"余额选中背景"] forState:UIControlStateSelected];
        }
        
    }
    
    UILabel *typeLab=[SingLeton myLabel:[UIColor color6] andTitle:@"支付方式" andFont:14];
    [self.view addSubview:typeLab];
    [typeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(titleLab.mas_bottom).mas_offset(175);
    }];
    
    
    UIImageView *zfbImg=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"支付宝"]];
    [self.view addSubview:zfbImg];
    [zfbImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(typeLab.mas_bottom).mas_offset(20);
    }];
    
    UILabel *zfbLab=[SingLeton myLabel:[UIColor color3] andTitle:@"支付宝" andFont:17];
    [self.view addSubview:zfbLab];
    [zfbLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(zfbImg);
        make.left.mas_equalTo(zfbImg.mas_right).mas_offset(10);
    }];
    
    
    UIButton *zfbButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:zfbButton];
    [zfbButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(zfbImg);
        make.right.mas_equalTo(-30);
        make.size.mas_equalTo(CGSizeMake(100, 30));
    }];
    
    [zfbButton setImage:[UIImage imageNamed:@"可选"] forState:UIControlStateNormal];
    [zfbButton setImage:[UIImage imageNamed:@"选择"] forState:UIControlStateSelected];
    zfbButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [zfbButton addTarget:self action:@selector(payAction:) forControlEvents:UIControlEventTouchUpInside];
    [self payAction:zfbButton];
    
    UIImageView *wxImg=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"微信支付"]];
    [self.view addSubview:wxImg];
    [wxImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(zfbImg.mas_bottom).mas_offset(20);
    }];
    
    UILabel *wxLab=[SingLeton myLabel:[UIColor color3] andTitle:@"微信支付" andFont:17];
    [self.view addSubview:wxLab];
    [wxLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(wxImg);
        make.left.mas_equalTo(wxImg.mas_right).mas_offset(10);
    }];
    
    
    UIButton *wxButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:wxButton];
    [wxButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(wxImg);
        make.right.mas_equalTo(-30);
        make.size.mas_equalTo(CGSizeMake(100, 30));
    }];
    
    [wxButton setImage:[UIImage imageNamed:@"可选"] forState:UIControlStateNormal];
    [wxButton setImage:[UIImage imageNamed:@"选择"] forState:UIControlStateSelected];
    wxButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [wxButton addTarget:self action:@selector(payAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *payButton=[SingLeton endButton:[UIColor colorWithHexString:@"09be00"] andTitle:@"充值" RequestSuccess:^(UIButton *btn) {
//        //应用注册scheme
//        NSString *appScheme = @"pandaBattery";
//        
//        //后台获取
//        NSString *orderString = @"";
//        
//        // NOTE: 调用支付结果开始支付
//        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
//            NSLog(@"reslut = %@",resultDic);
//        }];
        
        
        
        
        
        PayReq *request = [[PayReq alloc] init];
        /** 商家向财付通申请的商家id */
        request.partnerId = @"1220277201";
        /** 预支付订单 */
        request.prepayId= @"82010380001603250865be9c4c063c30";
        /** 商家根据财付通文档填写的数据和签名 */
        request.package = @"Sign=WXPay";
        /** 随机串，防重发 */
        request.nonceStr= @"lUu5qloVJV7rrJlr";
        /** 时间戳，防重发 */
        request.timeStamp= 1458893985;
        /** 商家根据微信开放平台文档对数据做的签名 */
        request.sign= @"b640c1a4565b476db096f4d34b8a9e71960b0123";
        /*! @brief 发送请求到微信，等待微信返回onResp
         *
         * 函数调用后，会切换到微信的界面。第三方应用程序等待微信返回onResp。微信在异步处理完成后一定会调用onResp。支持以下类型
         * SendAuthReq、SendMessageToWXReq、PayReq等。
         * @param req 具体的发送请求，在调用函数后，请自己释放。
         * @return 成功返回YES，失败返回NO。
         */
        [WXApi sendReq: request];
        
        
        
        
        
        
        
        
        
    }];
    [self.view addSubview:payButton];
    [payButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.bottom.mas_equalTo(-20);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-40, 44));
    }];
    
    
    
}
-(void)payAction:(UIButton *)btn
{
    
    if (_selectButton) {
        _selectButton.selected=NO;
    }
    
    _selectButton=btn;
    btn.selected=YES;
}
-(void)chooseAction:(UIButton *)btn
{
    if (_selectNumButton) {
        _selectNumButton.selected=NO;
    }
    
    _selectNumButton=btn;
    btn.selected=YES;
}

@end
